package com.suning.sawp.loadtest;

import com.google.common.collect.AbstractIterator;

/**
 * 固定商品循环
 * 
 * @author 13071602
 *
 */
public class MockCommGenerator extends AbstractIterator<com.suning.sawp.loadtest.MockCommGenerator.Comm> {
    public static class Comm {
        private String commType;
        private String cateCode;
        private String commCode;

        public Comm() {

        }

        public String getCommCode() {
            return commCode;
        }

        public void setCommCode(String commCode) {
            this.commCode = commCode;
        }

        public Comm(String cateCode, String commCode, String commType) {
            this.cateCode = cateCode;
            this.commCode = commCode;
            this.commType = commType;
        }

        public String getCommType() {
            return commType;
        }

        public void setCommType(String commType) {
            this.commType = commType;
        }

        public String getCateCode() {
            return cateCode;
        }

        public void setCateCode(String cateCode) {
            this.cateCode = cateCode;
        }

    }

    public MockCommGenerator(long maxI, long maxJ, boolean infinite) {
        this.maxI = maxI;
        this.maxJ = maxJ;
        this.infinite = infinite;
    }

    long commCode = 0xF00000000L;
    long cateCode = 0xF0000;

    final long maxI;
    final long maxJ;
    long i = 0;
    long j = 0;

    private Comm comm;
    boolean end;
    boolean infinite;

    /**
     * 是否结束
     * 
     * @return
     */
    private boolean add() {
        comm = new Comm(Long.toHexString(cateCode), Long.toHexString(commCode),
                new String[] { "A", "B" }[(int) (commCode % 2)]);
        commCode++;
        if (++i >= maxI) {
            i = 0;
            cateCode++;
            if (++j >= maxJ) {
                j = 0;
                return true;
            }
        }
        return false;
    }

    @Override
    protected Comm computeNext() {
        if (end) {
            return endOfData();
        }
        end = add() && !infinite;
        return comm;
    }

}
